package com.ac.scum.log.task.task;


import com.ac.scum.player.score.bean.PlayerScoreUnbomb;
import com.ac.scum.log.bean.PlayerUnbombScoreNotCounted;
import com.ac.scum.log.task.mapper.log.PlayerScoreUnbombMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class UnbombScoreTask {

    private final RedisTemplate<String, Object> redisTemplate;
    private final PlayerScoreUnbombMapper playerScoreUnbombMapper;

    @Scheduled(cron = "0 */2 * * * ?")
    public void unbombScore() {
        Set<String> keys = redisTemplate.keys("unbomb-score:*");
        if (keys == null || keys.isEmpty()) {
            return;
        }


        for (String key : keys) {
            List<Object> range = redisTemplate.opsForList().range(key, 0, -1);
            if (range == null || range.isEmpty()) {
                continue;
            }
            //获取到后删除
            redisTemplate.delete(key);
            List<PlayerUnbombScoreNotCounted> UnbombScoreNotCountedList = range.stream().map(item -> (PlayerUnbombScoreNotCounted) item).toList();

            for (PlayerUnbombScoreNotCounted playerUnbombScoreNotCounted : UnbombScoreNotCountedList) {
                PlayerScoreUnbomb playerUnbombScore = playerScoreUnbombMapper.selectOne(Wrappers.lambdaQuery(PlayerScoreUnbomb.class)
                        .eq(PlayerScoreUnbomb::getServerId, playerUnbombScoreNotCounted.getServerId())
                        .eq(PlayerScoreUnbomb::getPlayerId, playerUnbombScoreNotCounted.getPlayerId())
                        .eq(PlayerScoreUnbomb::getBombType, playerUnbombScoreNotCounted.getBombType())
                        .eq(PlayerScoreUnbomb::getCountDate, LocalDate.now()));
                if (playerUnbombScore == null) {
                    playerUnbombScore = new PlayerScoreUnbomb();
                    playerUnbombScore.setCountDate(LocalDate.now());
                    playerUnbombScore.setSteamId(playerUnbombScoreNotCounted.getSteamId());
                    playerUnbombScore.setServerId(playerUnbombScoreNotCounted.getServerId());
                    playerUnbombScore.setPlayerId(playerUnbombScoreNotCounted.getPlayerId());
                    playerUnbombScore.setBombType(playerUnbombScoreNotCounted.getBombType());
                    playerUnbombScore.setTotal(0);
                    playerUnbombScore.setTotalFailNum(0);
                    playerUnbombScore.setTotalSuccess(0);
                    playerUnbombScore.setTotalUseTime(0D);
                }
                playerUnbombScore.setTotal(playerUnbombScore.getTotal() + (playerUnbombScoreNotCounted.getFlag() ? 1 : 0) + playerUnbombScoreNotCounted.getFailNum());
                if (playerUnbombScore.getTotal() == 0) {
                    continue;
                }
                playerUnbombScore.setTotalUseTime(NumberUtils.toDouble(String.format("%.2f", (double) playerUnbombScore.getTotalUseTime() + playerUnbombScoreNotCounted.getUseTime())));
                playerUnbombScore.setTotalSuccess(playerUnbombScore.getTotalSuccess() + (playerUnbombScoreNotCounted.getFlag() ? 1 : 0));
                playerUnbombScore.setTotalFailNum(playerUnbombScore.getTotalFailNum() + playerUnbombScoreNotCounted.getFailNum());
                playerUnbombScore.setAvgUseTime(NumberUtils.toDouble(String.format("%.2f", (double) playerUnbombScore.getTotalUseTime() / playerUnbombScore.getTotal())));
                playerUnbombScore.setAvgFailNum(NumberUtils.toDouble(String.format("%.2f", (double) playerUnbombScore.getTotalFailNum() / playerUnbombScore.getTotal())));

                playerUnbombScore.setAvgSuccessNum(NumberUtils.toDouble(String.format("%.2f", (double) playerUnbombScore.getTotalSuccess() / playerUnbombScore.getTotal())));

                if (playerUnbombScore.getId() == null) {
                    playerScoreUnbombMapper.insert(playerUnbombScore);
                } else {
                    playerScoreUnbombMapper.updateById(playerUnbombScore);
                }
            }
        }
        log.info("剪线数据统计完成");
    }
}
